home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cgraphix / bezier.c < prev    next >
Text File  |  1986-05-15  |  2KB  |  66 lines

  1. /* «RM120»«PL99999»«TS4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76» */
  2. #include    <stdio.h>
  3. #define    EXTERN    extern
  4. #include    <typedef.h>
  5.  
  6. int bezier(A, MaxContrPoints, B, MaxIntPoints)
  7. double    *A, *B;
  8. int        MaxContrPoints, MaxIntPoints;
  9. {
  10.     extern char        *calloc();
  11.     extern double    pow();
  12.  
  13.     double    *combi, SumX, SumY, u, DeltaU;
  14.     double    mult, amult;
  15.     int        IntPoint;
  16.     int        i, j, k;
  17.  
  18.     /* construct the binomial coefficients                                    */
  19.  
  20.     if (NULL == (combi = (double *)calloc(MaxContrPoints, sizeof(double)))) {
  21.         fprintf(stderr, "Ran out of memory in Bezier - 1.\n");
  22.         return(-1);
  23.     }
  24.  
  25.     combi[0] = 1.;
  26.     combi[MaxContrPoints-1] = 1.;
  27.     j = (MaxContrPoints+1) / 2;
  28.     for (i = 1; i < j; i++) {
  29.         combi[i] = combi[i-1] * (MaxContrPoints - i) /(double)i;
  30.         combi[MaxContrPoints - 1 - i] = combi[i];
  31.     }
  32.  
  33.     /* initial point: u = 0                                                    */
  34.     B[0] = A[0];
  35.     B[1] = A[1];
  36.  
  37.     /* final point: u = 1                                                    */
  38.     B[2*MaxIntPoints-2] = A[2*MaxContrPoints-2];
  39.     B[2*MaxIntPoints-1] = A[2*MaxContrPoints-1];
  40.  
  41.     DeltaU = .5 / (MaxIntPoints - 1);            /* one half spacing            */
  42.  
  43.     for (i = 2; i < 2*MaxIntPoints-2; i+=2) {    /* loop through the inter-    */
  44.                                                 /*    polation points            */
  45.         u = i * DeltaU;
  46.         mult = pow(1.-u, (double)(MaxContrPoints-1));
  47.         amult = u / (1. - u);
  48.         SumX = 0.;
  49.         SumY = 0.;
  50.         k = 0;
  51.         for (j = 0; j < MaxContrPoints; j++) {    /* loop thru Control Pts    */
  52.             SumX = SumX + combi[j] * mult * A[k++];
  53.             SumY = SumY + combi[j] * mult * A[k++];
  54. /*            printf("%d %d %lf %lf %lf %lf %lf %lf\n", i, j, u, mult,
  55.                 A[2*j], A[2*j+1], SumX, SumY);
  56. */
  57.             mult = mult * amult;
  58.         }
  59.         B[i] = SumX;
  60.         B[i+1] = SumY;
  61.     }
  62.  
  63.     free(combi);
  64. }
  65.  
  66.